/*
 * @Author: yangzonglong
 * @Date: 2021-01-26 11:08:24
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-04-20 17:19:03
 * @Auditor: 
 */
import { AnyAction } from 'redux';
import { ResultProps } from './../components/result/Result';

export interface CommonState {
  collapsed: boolean,
  loading: boolean,
  openKeys: string[],
  commonResult: { visible: boolean, code: number; },
  fdModalFormVisible: boolean;
}

const Types = {
  TOGGLE_COLLAPSED: 'toggleCollapsed',
  SET_LOADING: 'setLoading',
  TOGGLE_OPENKEYS: 'toggleOpenKeys',
  SET_COMMON_RESULT: 'setCommonResult'
};

const initialState = {
  collapsed: sessionStorage.collapsed === '1',
  loading: false,
  commonResult: { visible: false, code: 403 },
  openKeys: [],
  fdModalFormVisible: false
};

export default (state: CommonState = initialState, action: AnyAction) => {
  switch (action.type) {
    case Types.TOGGLE_COLLAPSED:
      return { ...state, collapsed: action.data };

    case Types.SET_LOADING:
      return { ...state, loading: action.data };

    case Types.TOGGLE_OPENKEYS:
      return { ...state, openKeys: action.data };

    case Types.SET_COMMON_RESULT:
      return { ...state, commonResult: action.data };

    default:
      return state;
  }
};

export const toggleCollapsed = (collapsed: boolean) => {
  sessionStorage.collapsed = collapsed ? '1' : '0';
  return {
    type: Types.TOGGLE_COLLAPSED,
    data: collapsed
  };
};

export const setLoading = (loading: boolean) => {
  return {
    type: Types.SET_LOADING,
    data: loading
  };
};

export const toggleOpenKeys = (openKeys: string[]) => {
  return {
    type: Types.TOGGLE_OPENKEYS,
    data: openKeys
  };
};

export const setCommonResult = (data: ResultProps) => {
  return {
    type: Types.SET_COMMON_RESULT,
    data
  };
};